package simulatorGUI;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.SwingUtilities;

import everything.WorldMap;
import graphics.server.Mymain;



/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class Simulator extends javax.swing.JFrame {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private DefaultListModel jList1Model;
	private JPanel jPanel1;
	private JButton RunButton;
	private JButton quitButton1;
	private JCheckBox chkMemorize;
	private JLabel lblMemorize;
	private JTextField txtFlagValues;
	private JLabel lblFlagValues;
	private JTextField movesField;
	private JLabel moves;
	private JLabel lblP;
	private JTextField txtP;
	private JTextField txtBullets;
	private JLabel lblBullets;
	private JTextField txtDelay;
	private JLabel lblDelay;
	private JLabel lblPicture;
	private JLabel jLabel1;
	private JTextField txtPly;
	private JLabel RTMaxLabel;
	private JTextField YjTextField2;
	private JTextField XjTextField2;
	private JLabel posLabel2;
	private JComboBox colorCombo;
	private JLabel colorjLabel2;
	private JButton addButton1;
	private JComboBox direction;
	private JComboBox typeCombo;
	private JLabel typeLabel;
	private JTextField jTextField1;
	private JLabel NamejLabel1;
	private JPanel jPanel3;
	private JTextField FilejTextField2;
	private JButton BrowsejButton1;
	private JLabel mapjLabel2;
	private JList jList1;
	private JLabel numOfAgent;
	private JLabel lbldirection;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Simulator inst = new Simulator();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
		Thread t1 = new Thread(new Runnable(){
			public void run(){
				try {
					Mymain.main(new String[]{"Mymain"});
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		t1.start();
	}

	public Simulator() {
		super();
		initGUI();
	}

	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().add(getJPanel1(), BorderLayout.CENTER);
			pack();
			this.setSize(458, 604);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private DefaultListModel getJList1Model() {
		if(jList1Model == null) {
			jList1Model = new DefaultListModel();
		}
		return jList1Model;
	}

	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jPanel1 = new JPanel();
			FlowLayout jPanel1Layout = new FlowLayout();
			jPanel1.setLayout(jPanel1Layout);
			jPanel1.setPreferredSize(new java.awt.Dimension(461,323));
			jPanel1.setLayout(null);
			jPanel1.setBorder(new LineBorder(new java.awt.Color(0,0,0),1,false));
			jPanel1.add(getNumOfAgent());
			jPanel1.add(getJList1());
			jPanel1.add(getRunButton());
			jPanel1.add(getQuitButton1());
			jPanel1.add(getMapjLabel2());
			jPanel1.add(getBrowsejButton1());
			jPanel1.add(getFilejTextField2());
			jPanel1.add(getJPanel3());
			jPanel1.add(getJLabel1());
			jPanel1.add(getLblDelay());
			jPanel1.add(getTxtDelay());
			jPanel1.add(getLblBullets());
			jPanel1.add(getTxtBullets());
			jPanel1.add(getTxtP());
			jPanel1.add(getLblP());
			jPanel1.add(getMoves());
			jPanel1.add(getMovesField());
			jPanel1.add(getLblFlagValues());
			jPanel1.add(getTxtFlagValues());
		}
		return jPanel1;
	}
	private JLabel getNumOfAgent() {
		if (numOfAgent == null) {
			numOfAgent = new JLabel();
			numOfAgent.setText("Name    Color     Type                Pos     Direction     max_expansions");
			numOfAgent.setBounds(38, 237, 325, 14);
		}
		return numOfAgent;
	}
	private JList getJList1() {
		if(jList1 == null) {
			jList1 = new JList();
			jList1.setModel(getJList1Model());
			jList1.setBounds(38, 254, 365, 117);
			jList1.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
			//((DefaultListModel)jList1.getModel()).addElement("Walle Blue maximax 2 2 North 8 false");
			//((DefaultListModel)jList1.getModel()).addElement("Eva Red maximax 3 3 North 8 false");
//			((DefaultListModel)jList1.getModel()).addElement("Walle Blue pruning 2 2 North 8 true");
//			((DefaultListModel)jList1.getModel()).addElement("Eva Red pruning 3 3 North 8 true");
//			((DefaultListModel)jList1.getModel()).addElement("Walle Blue intelligent 1 1 North 6 true");
//			((DefaultListModel)jList1.getModel()).addElement("Eva Red intelligent 3 3 North 6 true");
//			((DefaultListModel)jList1.getModel()).addElement("Walle Blue maximax 2 2 North 6 true");
//			((DefaultListModel)jList1.getModel()).addElement("Eva Red maximax 3 3 North 6 true");
			((DefaultListModel)jList1.getModel()).addElement("Walle Blue maximaxpruning 1 1 North 8 true");
			((DefaultListModel)jList1.getModel()).addElement("Eva Red maximaxpruning 1 8 North 8 true");
//			((DefaultListModel)jList1.getModel()).addElement("Walle Blue probability 2 3 North 6 true");
//			((DefaultListModel)jList1.getModel()).addElement("Eva Red probability 2 2 North 6 true");
		}
		return jList1;
	}
	private JButton getRunButton() {
		if(RunButton == null) {
			RunButton = new JButton();
			RunButton.setText("RUN");
			RunButton.setBounds(92, 521, 91, 21);
			RunButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					//System.out.println("RunButton.actionPerformed, event="+evt);
					File f = new File(FilejTextField2.getText());
					if (f.exists()){
						new Thread(new Runnable(){
							// run program
							public void run(){
								//int flagValues = Integer.parseInt(txtFlagValue.getText());
								int delay = (int)(Double.parseDouble(txtDelay.getText())*1000.0);
								int bullets = Integer.parseInt(txtBullets.getText());
								double probability = Double.parseDouble(txtP.getText());
								int moves =  Integer.parseInt(movesField.getText());
								String[] flagValues = txtFlagValues.getText().split(",");
								int[] intFlagValues = new int[flagValues.length];
								try{
									for (int i=0; i<intFlagValues.length; i++){
										intFlagValues[i] = Integer.parseInt(flagValues[i]);
									}
								} catch (NumberFormatException e) {
									JOptionPane.showMessageDialog(null, "Invalid flag values - all flag values are changed to 10");
									intFlagValues = null;
//									for (int i=0; i<intFlagValues.length; i++){
//										intFlagValues[i] = 10;
//									}
								}

								WorldMap m = new WorldMap(FilejTextField2.getText(), intFlagValues, delay, probability, bullets, moves);
								Object[] agentDataObject = ((DefaultListModel)jList1.getModel()).toArray();
								String[] agentData = new String[agentDataObject.length];

								for (int i = 0; i<agentData.length; i++){
									agentData[i] = (String)agentDataObject[i];
								}
								m.runWorld(agentData);
							}
						}).start();
						dispose();
					} else {
						JOptionPane.showMessageDialog(null, "Invalid file");
					}
					//setVisible(false);
				}
			});
		}
		return RunButton;
	}
	private JButton getQuitButton1() {
		if(quitButton1 == null) {
			quitButton1 = new JButton();
			quitButton1.setText("Exit");
			quitButton1.setBounds(262, 521, 77, 21);
			quitButton1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					//System.out.println("quitButton1.actionPerformed, event="+evt);
					System.exit(0);
				}
			});
		}
		return quitButton1;
	}
	private JLabel getMapjLabel2() {
		if(mapjLabel2 == null) {
			mapjLabel2 = new JLabel();
			mapjLabel2.setText("Map");
			mapjLabel2.setBounds(33, 492, 29, 14);
		}
		return mapjLabel2;
	}
	private JButton getBrowsejButton1() {
		if(BrowsejButton1 == null) {
			BrowsejButton1 = new JButton();
			BrowsejButton1.setText("Browse");
			BrowsejButton1.setBounds(333, 489, 49, 21);
			BrowsejButton1.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					JFileChooser fileChooser = new JFileChooser();
					fileChooser.showDialog(null, "Open");
					if (fileChooser.getSelectedFile()!=null) FilejTextField2.setText(fileChooser.getSelectedFile().getPath());
				}
			});
		}
		return BrowsejButton1;
	}
	private JTextField getFilejTextField2() {
		if(FilejTextField2 == null) {
			FilejTextField2 = new JTextField();
			FilejTextField2.setText("board1.txt");
			FilejTextField2.setBounds(62, 489, 256, 21);
		}
		return FilejTextField2;
	}
	private JPanel getJPanel3() {
		if(jPanel3 == null) {
			jPanel3 = new JPanel();
			jPanel3.setLayout(null);
			jPanel3.setBounds(38, 31, 370, 200);
			jPanel3.setEnabled(false);
			jPanel3.setBorder(new LineBorder(new java.awt.Color(0,0,0),1,false));
			jPanel3.add(getNamejLabel1());
			jPanel3.add(getJTextField1());
			jPanel3.add(getLblMemorize());
			jPanel3.add(getChkMemorize());
			jPanel3.add(getLblPicture());
			jPanel3.add(getRTMaxLabel());
			jPanel3.add(getRTMaxjTextField2());
			jPanel3.add(getTypeLabel());
			jPanel3.add(getTypeCombo());
			jPanel3.add(getDirection());
			jPanel3.add(getDirectoin());
			jPanel3.add(getAddButton1());
			jPanel3.add(getColorjLabel2());
			jPanel3.add(getColorCombo());
			jPanel3.add(getPosLabel2());
			jPanel3.add(getXjTextField2());
			jPanel3.add(getYjTextField2());
		}
		return jPanel3;
	}
	private JLabel getNamejLabel1() {
		if(NamejLabel1 == null) {
			NamejLabel1 = new JLabel();
			NamejLabel1.setText("Name");
			NamejLabel1.setBounds(7, 5, 37, 26);
		}
		return NamejLabel1;
	}
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jPanel1.add(getJPanel3());
			//jPanel1.add(getName());
			jTextField1.setBounds(164, 8, 101, 21);
		}
		return jTextField1;
	}
	private JLabel getTypeLabel() {
		if(typeLabel == null) {
			typeLabel = new JLabel();
			typeLabel.setText("Type");
			typeLabel.setBounds(7, 61, 37, 18);
		}
		return typeLabel;
	}
	private JComboBox getTypeCombo() {
		if(typeCombo == null) {
			ComboBoxModel typeComboModel =
				new DefaultComboBoxModel(
						new String[] { "human", "intelligent", "pruning", "maximax", "maximaxpruning", "probability"});
			typeCombo = new JComboBox();
			typeCombo.setModel(typeComboModel);
			typeCombo.setBounds(164, 60, 197, 21);
			typeCombo.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					//System.out.println("typeCombo.actionPerformed, event="+evt);
					if (((String)typeCombo.getSelectedItem()).compareToIgnoreCase("intelligent") == 0){
						txtPly.setEnabled(true);
					} else {
					//	txtPly.setEnabled(false);
					//	txtPly.setText("");
					}

				}
			});
		}
		return typeCombo;
	}
	private JLabel getDirection() {
		if(lbldirection == null) {
			lbldirection = new JLabel();
			lbldirection.setText("Starting Direction");
			lbldirection.setBounds(7, 93, 83, 14);
		}
		return lbldirection;
	}
	private JComboBox getDirectoin() {
		if(direction == null) {
			ComboBoxModel directoinModel =
				new DefaultComboBoxModel(
						new String[] { "North", "South","West","East" });
			direction = new JComboBox();
			direction.setModel(directoinModel);
			direction.setBounds(162, 90, 76, 21);
		}
		return direction;
	}
	private JButton getAddButton1() {
		if(addButton1 == null) {
			addButton1 = new JButton();
			addButton1.setText("Add Agent");
			addButton1.setBounds(256, 142, 90, 21);
			addButton1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					//System.out.println("addButton1.actionPerformed, event="+evt);
					String tmp;
					tmp = jTextField1.getText();
					tmp = tmp + "   " + colorCombo.getSelectedItem();
					tmp = tmp + "   " + typeCombo.getSelectedItem();
					tmp = tmp + "   " + XjTextField2.getText();
					tmp = tmp + " " + YjTextField2.getText();
					tmp = tmp + "   " + direction.getSelectedItem();
					if (txtPly.isEnabled()){
						tmp = tmp + "   " + txtPly.getText();
						tmp = tmp + "   " + chkMemorize.isSelected();
					}

					jTextField1.setText("");
					colorCombo.setSelectedIndex(0);
					typeCombo.setSelectedIndex(0);
					XjTextField2.setText("");
					YjTextField2.setText("");
					direction.setSelectedIndex(0);
					txtPly.setText("");
					jTextField1.requestFocus();
					((DefaultListModel)jList1.getModel()).addElement(tmp);
					jList1.repaint();
				}
			});
		}
		return addButton1;
	}
	private JLabel getColorjLabel2() {
		if(colorjLabel2 == null) {
			colorjLabel2 = new JLabel();
			colorjLabel2.setText("Color");
			colorjLabel2.setBounds(7, 35, 25, 14);
		}
		return colorjLabel2;
	}
	private JComboBox getColorCombo() {
		if(colorCombo == null) {
			ComboBoxModel colorComboModel =
				new DefaultComboBoxModel(
						new String[] { "Blue", "Red" });
			colorCombo = new JComboBox();
			colorCombo.setModel(colorComboModel);
			colorCombo.setBounds(164, 32, 74, 21);
			lblPicture.setIcon(getImage("images/AgentNorth.jpg"));
			colorCombo.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					if ("Blue".equals(colorCombo.getSelectedItem())){
						lblPicture.setIcon(getImage("images/AgentNorth.jpg"));
					} else if ("Red".equals(colorCombo.getSelectedItem())){
						lblPicture.setIcon(getImage("images/AgentNorthRed.jpg"));
					}
				}
			});
		}
		return colorCombo;
	}
	private JLabel getPosLabel2() {
		if(posLabel2 == null) {
			posLabel2 = new JLabel();
			posLabel2.setText("Starting Position");
			posLabel2.setBounds(7, 124, 78, 14);
		}
		return posLabel2;
	}
	private JTextField getXjTextField2() {
		if(XjTextField2 == null) {
			XjTextField2 = new JTextField();
			XjTextField2.setText("X");
			XjTextField2.setBounds(163, 121, 20, 21);
		}
		return XjTextField2;
	}
	private JTextField getYjTextField2() {
		if(YjTextField2 == null) {
			YjTextField2 = new JTextField();
			YjTextField2.setText("Y");
			YjTextField2.setBounds(189, 121, 20, 21);
		}
		return YjTextField2;
	}

	private JLabel getRTMaxLabel() {
		if(RTMaxLabel == null) {
			RTMaxLabel = new JLabel();
			RTMaxLabel.setText("Intelligent agent's rounds");
			RTMaxLabel.setBounds(7, 151, 123, 14);
		}
		return RTMaxLabel;
	}

	private JTextField getRTMaxjTextField2() {
		if(txtPly == null) {
			txtPly = new JTextField();
			txtPly.setText("   ");
			txtPly.setBounds(163, 148, 33, 21);
			txtPly.setEnabled(false);
		}
		return txtPly;
	}

	private JLabel getJLabel1() {
		if(jLabel1 == null) {
			jLabel1 = new JLabel();
			jLabel1.setText("Agents");
			jLabel1.setBounds(38, 12, 34, 14);
		}
		return jLabel1;
	}

	private JLabel getLblPicture() {
		if(lblPicture == null) {
			lblPicture = new JLabel();
			lblPicture.setBounds(301, 11, 52, 43);
		}
		return lblPicture;
	}

	private JLabel getLblDelay() {
		if(lblDelay == null) {
			lblDelay = new JLabel();
			lblDelay.setText("Delay(Sec):");
			lblDelay.setBounds(179, 459, 59, 14);
		}
		return lblDelay;
	}

	private JTextField getTxtDelay() {
		if(txtDelay == null) {
			txtDelay = new JTextField();
			txtDelay.setText("0.5");
			txtDelay.setBounds(272, 456, 36, 20);
		}
		return txtDelay;
	}

	/*
	private JLabel getName() {
		if(name == null) {
			name = new JLabel();
			name.setText("name");
			name.setBounds(146, 120, 372, 189);
		}
		return name;
	}

	private JLabel getNamex() {
		if(name == null)		return name;
	}
	 */

	private ImageIcon getImage(String path){
		URL url = getClass().getResource("/" + path);
		if (url == null){
			return new ImageIcon(path);
		} else {
			try {
				return new ImageIcon(ImageIO.read(url));
			} catch (IOException e) {
				return new ImageIcon(path);
			}
		}
	}

	 private JLabel getLblBullets() {
		 if(lblBullets == null) {
			 lblBullets = new JLabel();
			 lblBullets.setText("Bullets:");
			 lblBullets.setBounds(33, 459, 35, 14);
		 }
		 return lblBullets;
	 }

	 private JTextField getTxtBullets() {
		 if(txtBullets == null) {
			 txtBullets = new JTextField();
			 txtBullets.setText("0");
			 txtBullets.setBounds(102, 456, 36, 20);
		 }
		 return txtBullets;
	 }

	 private JTextField getTxtP() {
		 if(txtP == null) {
			 txtP = new JTextField();
			 txtP.setText("0");
			 txtP.setBounds(272, 416, 36, 20);
		 }
		 return txtP;
	 }

	 private JLabel getLblP() {
		 if(lblP == null) {
			 lblP = new JLabel();
			 lblP.setText("P to stop slipping:");
			 lblP.setBounds(179, 419, 86, 14);
		 }
		 return lblP;
	 }

	 private JLabel getMoves() {
		 if(moves == null) {
			 moves = new JLabel();
			 moves.setText("moves");
			 moves.setBounds(33, 419, 56, 14);
		 }
		 return moves;
	 }

	 private JTextField getMovesField() {
		 if(movesField == null) {
			 movesField = new JTextField();
			 movesField.setText("50");
			 movesField.setBounds(102, 416, 36, 20);
		 }
		 return movesField;
	 }

	 private JLabel getLblFlagValues() {
		 if(lblFlagValues == null) {
			 lblFlagValues = new JLabel();
			 lblFlagValues.setText("Flag values (seperated with ',' - no spaces)");
			 lblFlagValues.setBounds(33, 391, 206, 14);
		 }
		 return lblFlagValues;
	 }

	 private JTextField getTxtFlagValues() {
		 if(txtFlagValues == null) {
			 txtFlagValues = new JTextField();
			 txtFlagValues.setBounds(249, 388, 154, 20);
		 }
		 return txtFlagValues;
	 }
	 
	 private JLabel getLblMemorize() {
		 if(lblMemorize == null) {
			 lblMemorize = new JLabel();
			 lblMemorize.setText("Memorize");
			 lblMemorize.setBounds(7, 176, 45, 14);
		 }
		 return lblMemorize;
	 }
	 
	 private JCheckBox getChkMemorize() {
		 if(chkMemorize == null) {
			 chkMemorize = new JCheckBox();
			 chkMemorize.setBounds(163, 173, 21, 21);
		 }
		 return chkMemorize;
	 }
}
